package com.hfjyz.expert.expertcall.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.expert.expert.bean.ExpertBean;
import com.hfjyz.expert.expertcall.bean.ExpertCallBean;
import com.hfjyz.expert.expertcall.bean.ExpertCallSearchBean;
import com.hfjyz.expert.expertcall.bean.ExpertCallSearchCondition;
import com.hfjyz.expert.expertcall.service.ExpertCallService;

public class ExpertCallAction extends GridBaseAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -329879627858090900L;
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);
    private ExpertCallService service = null;
    private ExpertCallBean expertCall = null;
    private String selectIds = null;
    private Integer expertId = null;
    protected ExpertCallSearchBean expertCallSearch = null;

    public ExpertCallService getService() {
        return service;
    }

    public void setService(ExpertCallService service) {
        this.service = service;
    }

    public ExpertCallBean getExpertCall() {
        return expertCall;
    }

    public void setExpertCall(ExpertCallBean expertCall) {
        this.expertCall = expertCall;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public Integer getExpertId() {
        return expertId;
    }

    public void setExpertId(Integer expertId) {
        this.expertId = expertId;
    }

    public ExpertCallSearchBean getExpertCallSearch() {
        return expertCallSearch;
    }

    public void setExpertCallSearch(ExpertCallSearchBean expertCallSearch) {
        this.expertCallSearch = expertCallSearch;
    }

    @Override
    protected List<ExpertCallBean> getBeanList() throws BaseException {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute(BaseConstant.SESSION_CONDITION_KEY, expertCallSearch);

        return service.findExpertCall(getSearchCondition(), expertCallSearch);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getExpertCallCount(expertCallSearch);
    }

    public String save() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            service.saveExpertCall(expertCall);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            if (selectIds == null) {
                SYS_LOG.error("未选择待删除的专家调用！");
                throw new BaseException("未选择待删除的专家调用！");
            }
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteExpertCall(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String updateInit() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            expertCall = service.updateInitExpertCall(id);
            List<ExpertBean> expertList = service.findAllExpert();

            map.put("expertCall", expertCall);
            map.put("expertList", expertList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String listExpert() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            List<ExpertBean> expertList = service.findAllExpert();
            map.put("expertList", expertList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public SearchCondition getSearchCondition() {
        SearchCondition cond = new ExpertCallSearchCondition();
        cond.setPageNum(pageNum);
        cond.setRowCountPerPage(rowCountPerPage);
        cond.setSortColumn(sortColumn);
        cond.setSortDirection(sortDirection);

        return cond;
    }
}
